package com.jyh.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jyh.config.BaseAction;
import com.jyh.db.entity.JyhCatalogue;
import com.jyh.db.entity.JyhCatalogueExample;
import com.jyh.db.entity.JyhNavigate;
import com.jyh.db.entity.JyhNavigateExample;
import com.jyh.db.mapper.JyhCatalogueMapper;
import com.jyh.db.mapper.JyhNavigateMapper;

@Service
public class MenuService extends BaseAction {

	@Autowired
	private JyhCatalogueMapper catalogueMapper;

	@Autowired
	private JyhNavigateMapper navigateMapper;

	/**
	 * 目录生成
	 */
	public String getCatalogueFirst(HttpServletRequest request, int cid, HttpSession session) {
		// 获取权限
		Map<String, List<String>> listSession = (Map<String, List<String>>) session.getAttribute("jurisdiction");
		if (listSession == null) {
			return "";
		}
		List<String> listStr = listSession.get("left");

		String catMenu = "";
		JyhCatalogueExample catalogueExample = new JyhCatalogueExample();
		catalogueExample.createCriteria().andCidEqualTo(cid).andFidEqualTo(0).andIsLookEqualTo(1);
		catalogueExample.setOrderByClause("ID ASC");
		List<JyhCatalogue> cata = catalogueMapper.selectByExample(catalogueExample);
		for (int i = 0; i < cata.size(); i++) {
			if (cata.get(i).getFid() == 0) {
				catMenu += "<div class='sidebar-nav'><div class='sidebar-title'><a href='#'>"
						+ "<span class='icon'><b class='fl icon-arrow-down'></b></span><span"
						+ " class='text-normal'><strong>" + cata.get(i).getCatalogueName()
						+ "</strong></span></a></div>";

				catalogueExample = new JyhCatalogueExample();
				catalogueExample.createCriteria().andCidEqualTo(cid).andFidEqualTo(cata.get(i).getId()).andIsLookEqualTo(1);;
				catalogueExample.setOrderByClause("ID ASC");
				List<JyhCatalogue> cataSencd = catalogueMapper.selectByExample(catalogueExample);
				if (cataSencd.size() > 0) {
					catMenu += "<ul class='sidebar-trans'>";
					for (int j = 0; j < cataSencd.size(); j++) {
						if (listStr.contains(cataSencd.get(j).getId()+"")) {
							catMenu += "<li><a href='" + request.getContextPath() + cataSencd.get(j).getCatalogueUrl()
									+ "'  target='main'><b class='sidebar-icon'>" + "<img src='../Images/"
									+ cataSencd.get(j).getIco() + ".png' width='16' height='16' />"
									+ "</b><span class='text-normal'>" + cataSencd.get(j).getCatalogueName()
									+ "</span></a></li>";
						}
					}
					catMenu += "</ul>";
				}
			}
			catMenu += "</div>";
		}
		return catMenu;
	}

	/**
	 * 头导航
	 * 
	 * @return
	 */
	public String getNavigate(HttpServletRequest request, HttpSession session) {
//		Map<String, List<String>> listSession = (Map<String, List<String>>) session.getAttribute("jurisdiction");
//		List<String> listStr = listSession.get("head");
		String catMenu = "<ul class=\"hr\">";
		JyhNavigateExample navigateExample = new JyhNavigateExample();
		navigateExample.createCriteria().andIsLookEqualTo(1);
		navigateExample.setOrderByClause("ID ASC");
		List<JyhNavigate> cata = navigateMapper.selectByExample(navigateExample);
		for (int i = 0; i < cata.size(); i++) {
//			if (listStr.contains(cata.get(i).getId()+"")) {
				catMenu += "<li class=\"fl topbar-info-item\"><a href=\"" + request.getContextPath()
						+ cata.get(i).getFunParameters() + "\" class=\"topbar-btn topas" + i
						+ "\"  target='left'> <span class=\"fl text-normal\">" + cata.get(i).getNavName()
						+ "</span></a></li>";
//			}
		}
		catMenu += "</ul>";
		return catMenu;
	}
}
